package com.msos.dao.sendMsgDao;

import com.msos.model.customer.Customer;
import com.msos.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class sendMsgDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDs());
    public Customer getCustomerByPho(String acPhoNumber) throws SQLException {
        return queryRunner.query("select * from customer where phone=?",new BeanHandler<>(Customer.class),acPhoNumber);
    }

    public Integer sendMsg(String acPhoNumber, String msg, String sePhoNumber) throws SQLException {
        String sql="INSERT into message(sephone,acphone,content,sendDate,enabled) VALUES(?,?,?,?,?)";
        Connection con = DBUtils.getCon();
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy--MM--dd:HH--mm--ss");
        String day=simpleDateFormat.format(date);
        Integer enabled=1;
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1,sePhoNumber);
        ps.setString(2,acPhoNumber);
        ps.setString(3,msg);
        ps.setString(4,day);
        ps.setInt(5,enabled);
        Integer r1=ps.executeUpdate();
        //
      // return queryRunner.execute("INSERT into message(sephone,acphone,content,sendDate,enabled) VALUES(?,?,?,?,?)",new BeanHandler<>(Customer.class),sePhoNumber,acPhoNumber,msg,day,enabled);
        return r1;
    }
}
